package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Sport;
import com.itheima.service.SportService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/sport")
public class SportController {
    @Reference
    private SportService sportService;
    
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            List<Sport> data = new ArrayList<>();
            if (list != null && list.size() > 0) {
                for (String[] strings : list) {
                    Integer sportId = Integer.parseInt(strings[0]);
                    String number = strings[1];
                    String name = strings[2];
                    Integer risk = Integer.parseInt(strings[3]);
                    Integer typesOfExercise = Integer.parseInt(strings[4]);
                    String time = strings[5];
                    String frequency = strings[6];
                    Integer type = Integer.parseInt(strings[7]);
                    String strength = strings[8];
                    String met = strings[9];
                    String tse = strings[10];
                    String tset = strings[11];
                    Integer minAge = Integer.parseInt(strings[12]);
                    Integer maxAge = Integer.parseInt(strings[13]);
                    String actionPart = strings[14];
                    String actionMethod = strings[15];
                    Integer enable = Integer.parseInt(strings[16]);
                    String burnCalories = strings[17];
                    String consumeEnergy = strings[18];
                    Sport sport = new Sport(null, sportId, number, name, risk,
                            typesOfExercise, time, frequency, type,
                            strength, met, tse, tset, minAge,
                            maxAge, actionPart, actionMethod,enable,burnCalories,consumeEnergy);
                    data.add(sport);
                }
            }
            sportService.excelAdd(data);
            return new Result(true, MessageConstant.IMPORT_SPORTDATA_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_SPORTDATA_FAIL);
        }
        
    }
    
    @RequestMapping("/add")
    public Result add(@RequestBody Sport sport) {
        try {
            sportService.add(sport);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SPORT_FAIL);
        }
        return new Result(true, MessageConstant.ADD_SPORT_SUCCESS);
    }
    
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = sportService.findPage(queryPageBean);
        return pageResult;
    }
    
    
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            sportService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_SPORT_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_SPORT_SUCCESS);
    }
    
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Sport sport = sportService.findById(id);
            return new Result(true, MessageConstant.QUERY_SPORT_SUCCESS, sport);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SPORT_FAIL);
        }
    }
    
    @RequestMapping("/edit")
    public Result edit(@RequestBody Sport sport) {
        try {
            sportService.edit(sport);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_SPORT_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_SPORT_SUCCESS);
    }
    
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Sport> list = sportService.findAll();
            return new Result(true, MessageConstant.QUERY_SPORT_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SPORT_FAIL);
        }
    }
}
